﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using UI.ParentForm;
using Components;
using UI.Lib.Factory;
using System.Collections;
using UI.Lib;

namespace UI.Sysset
{
    public partial class FrmPerRole : BaseTEdit
    {
        DataTable savedata = null;

        public FrmPerRole(string modulecode, string action)
            : base(modulecode, action)
        {
            InitializeComponent();

            //回写的方法
            this.SaveCallFuntion = "Sys0a05Manager$SaveData"; //保存的方法
            IsAfterSaveCloseWin = false;
        }

        //提取明细按钮点击时
        protected override void TakeBtnClick()
        {
            //根据用户选择的角色读取权限
            string rolecode = Validation.ToString(this.tbSummary.GetValue("rolecode"));
            this.treeMain.Nodes.Clear(); //清空树

            Hashtable where = new Hashtable();
            where["@usercode"] = "";
            where["@rolecode"] = rolecode;
            where["@enable"] = Context.IsSuperUser ? "x" : "2";

            //加载顶层节点
            DataTable dtRoot = CommonHelper.Entity.ExecFun("getallpermision", where);
            if (null == dtRoot || 0 == dtRoot.Rows.Count)
            {
                MessageBox.Show("您没有可以分配的权限！", "提示");
                return;
            }
            DataView view = null;
            TreeNode n1 = null;
            object parentcode = null;
            TreeNode node = null;
            TreeNode n2 = null;

            foreach (DataRow drRoot in dtRoot.Rows)
            {
                parentcode = drRoot["parentcode"];

                if (null != parentcode && "" != parentcode.ToString()) //是父节点
                    continue;

                node = new TreeNode();
                node.Text = (null == drRoot["modulename"]) ? "暂无" : drRoot["modulename"].ToString() + "(显示功能)";
                node.Name = drRoot["modulecode"].ToString();
                node.Checked = ("1" == drRoot["flg"].ToString());

                view = new DataView(dtRoot);
                view.RowFilter = string.Format("parentcode='{0}'", Validation.ToString(drRoot["modulecode"]));

                for (int i = 0; i < view.Count; i++)
                {
                    n1 = new TreeNode();
                    n1.Text = (null == view[i]["modulename"]) ? "暂无" : view[i]["modulename"].ToString() + "(显示功能)";
                    n1.Name = node.Name + "$" + view[i]["modulecode"].ToString();
                    n1.Checked = ("1" == view[i]["flg"].ToString());

                    if (null == view[i]["plist"])
                    {
                        node.Nodes.Add(n1);
                        continue;
                    }
                    //权限列表
                    string[] plist = view[i]["plist"].ToString().Split(',');
                    string[] owerList = view[i]["act"].ToString().Split(',');
                    foreach (string p in plist)
                    {
                        string[] item = p.Split('|');
                        if ("V" == item[0]) continue; //父节点代表显示

                        n2 = new TreeNode();
                        n2.Text = item[1];
                        n2.Name = n1.Name + "$" + item[0]; //特殊标记
                        n2.Checked = StringUtil.Entity.InArray(item[0], false, owerList);

                        n1.Nodes.Add(n2);
                    }

                    node.Nodes.Add(n1);
                }

                this.treeMain.Nodes.Add(node);
            }

            this.treeMain.ExpandAll();
        }

        protected override DataTable SetBackTreeData()
        {
            string rolecode = Validation.ToString(this.tbSummary.GetValue("rolecode"));

            if (null != savedata)
                savedata.Rows.Clear();

            //创建回写数据的DataTable
            savedata = DataTableHelper.Entity.MarkColumn(savedata, "actioncode", "modulecode", "rolecode");

            if (savedata == null)
            {
                MessageBox.Show("程序异常错误！", "提示");
                savedata = null;
                this.hasError = true;
                return null;
            }

            DataRow dr = null;
            //得到界面上用户选择的权限节点
            foreach (TreeNode tv in this.treeMain.Nodes)
            {
                if (!tv.Checked) continue;

                string[] pitem = tv.Name.Split(new char[] { '$' }, StringSplitOptions.RemoveEmptyEntries); //目前权限目录最多都是三层

                dr = savedata.NewRow();
                dr["actioncode"] = "V";
                dr["modulecode"] = pitem[0];
                dr["rolecode"] = rolecode;

                savedata.Rows.Add(dr);

                GetFatherNode(tv, rolecode); //保存子节点
            }

            return savedata;
        }

        //第二级
        private void GetFatherNode(TreeNode node, string rolecode)
        {
            DataRow dr = null;
            foreach (TreeNode tv in node.Nodes)
            {
                if (!tv.Checked) continue;

                string[] pitem = tv.Name.Split(new char[] { '$' }, StringSplitOptions.RemoveEmptyEntries); //目前权限目录最多都是三层

                if (pitem.Length < 1) continue;

                dr = savedata.NewRow();
                dr["actioncode"] = "V";
                dr["modulecode"] = pitem[1];
                dr["rolecode"] = rolecode;

                savedata.Rows.Add(dr);

                GetChildNodes(tv, rolecode);
            }
        }

        //二级以下
        private void GetChildNodes(TreeNode node, string rolecode)
        {
            DataRow dr = null;
            foreach (TreeNode tv in node.Nodes)
            {
                if (!tv.Checked) continue;

                string[] pitem = tv.Name.Split(new char[] { '$' }, StringSplitOptions.RemoveEmptyEntries); //目前权限目录最多都是三层

                if (pitem.Length < 3) continue;

                dr = savedata.NewRow();
                dr["actioncode"] = pitem[2];
                dr["modulecode"] = pitem[1];
                dr["rolecode"] = rolecode;

                savedata.Rows.Add(dr);
            }
        }

        protected override void AfterSaveData(bool isSuccess)
        {
            if (isSuccess)
            {
                this.treeMain.Nodes.Clear();
            }
        }

        public override bool BeforePopWin(string name, ref string where, ref string[] others)
        {
            if ("rolecode" == name)
            {
                if (!Context.IsSuperUser)
                {
                    where = string.Format(" (comcode = '{0}' OR comcode IS NULL) ", Context.User.Comcode);
                }
            }

            return true;
        }
    }
}
